package com.basic.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.basic.shiro.CustomerRealm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
@Slf4j
public class MyShiroConfig {

    //shiroFilterFactoryBean: 3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        filterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //设置登录请求
        filterFactoryBean.setLoginUrl("/toLogin");
        //登录成功后要跳转的链接
        filterFactoryBean.setUnauthorizedUrl("/toLogin");
        // 未授权界面;
        filterFactoryBean.setUnauthorizedUrl("/toUnauthorized");
        //添加shiro 的内置过滤器
        /**
         * anon ： 无需认证就可以访问
         * authc：必须认证才可以访问
         * user：必须拥有 记住我 才可以访问
         * perms ： 拥有对某个资源的权限才能访问
         * role ： 拥有某个角色权限才能访问
         */
        //拦截
        Map<String ,String > filterMap = new LinkedHashMap<>();
        filterMap.put("/toArea","authc");
        filterMap.put("/toUser","authc");

        // 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
       // filterMap.put("/logout", "logout");
        //授权：正常的情况下，没有授权会跳转到为授权页面
        filterMap.put("/toArea","perms[area]");
        filterMap.put("/toUser","perms[user]");

        filterFactoryBean.setFilterChainDefinitionMap(filterMap);
//        //设置授权成功登录界面
//        filterFactoryBean.setSuccessUrl("/index");
        return filterFactoryBean;

    }

    //DefaultWebSecurityManager ：2 接管 @Qualifier
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customerRealm") CustomerRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //引入自定义的内容 sessionManager
        securityManager.setSessionManager(sessionManager());
        //关联 customerRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //处理登录 ;jsessionid=EE6D5F3C4F0F5D694BAE695DB02A2E6C
    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 为了解决输入网址地址栏出现 jsessionid 的问题
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    //常见realm 对象，需要自定义：1
    @Bean(name = "customerRealm")
    public CustomerRealm customerRealm(){
        return new CustomerRealm();
    }

    // 整合ShiroDialect：用来配置shiro thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }



}
